#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author: 高建平
# @description: 
#
from typing import List
from sqlalchemy import Row
from sqlalchemy.orm import Session
import copy
from app.support import datetime_util
from app.models.dto.project_sale_strategy import ProjectSaleStrategyQueryReq, ProjectSaleStrategyReq,ProjectSaleStrategyResp
from app.dao.project_sale_strategy import ProjectSaleStrategyDao
from app.models.po.project_sale_strategy_po import ProjectSaleStrategyPo
from app.models.po.sub_project import SubProject
from app.models.po.project_base_po import ProjectBasePo
from app.models.po.project_planning_task_po import ProjectPlanningTaskPo

class ProjectSaleStrategyService():
    
    def get_sub_project_list(self,request: ProjectSaleStrategyQueryReq,db: Session):
        '''
         查询子项目策略
        '''
        projectSaleStrategyPoList = ProjectSaleStrategyDao().select_sub_project_list(request,db) 
        return self.sale_strategy_po_2_list(projectSaleStrategyPoList)
    
    
    def get_sub_project_snapshot_list(self,request: ProjectSaleStrategyQueryReq,db: Session):
        '''
         查询子项目策略历史快照列表
        '''
        rowList:List[Row] = ProjectSaleStrategyDao().select_sub_project_snapshot_list(request,db) 
        return self.row_2_list(rowList)
    
    def get_sub_project_panel_list(self,request: ProjectSaleStrategyQueryReq,db: Session):
        '''
         查询子项目策略看版
        '''
        projectSaleStrategyPoList = ProjectSaleStrategyDao().select_sub_project_panel_list(request,db) 
        
        return self.row_2_list(projectSaleStrategyPoList)
    
    def get_to_first_launch_project_panel(self,request: ProjectSaleStrategyQueryReq,db: Session):
        '''
         即将首发项目看版
        '''
        projectSaleStrategyPoList = ProjectSaleStrategyDao().select_to_first_launch_project_panel_1(request,db) 
        
        return self.row_2_list(projectSaleStrategyPoList)
    
    
    def row_2_list(self,rowList:List[Row]) -> List[ProjectSaleStrategyResp]:
        '''
         数据库row转
        '''
        list = []
        for row in rowList:
            resp:ProjectSaleStrategyResp = self.convert_base(row._get_by_key_impl_mapping(ProjectSaleStrategyPo))
            if row.__contains__(SubProject):
                self.set_sub_project(resp,row._get_by_key_impl_mapping(SubProject))
            list.append(resp)
        return list
    
    def sale_strategy_po_2_list(self,list:List[ProjectSaleStrategyPo]) -> List[ProjectSaleStrategyResp]:
        '''
         
        '''
        list = []
        for projectSaleStrategyPo in list:
            list.append(self.convert_base(projectSaleStrategyPo))
        return list
            
    
    
    def set_sub_project(self,resp:ProjectSaleStrategyResp,subProject:SubProject):
        '''
         设置子项目信息
        '''
        resp.subProjectName = subProject.subproject_id
        resp.projectId = subProject.project_id
        resp.projectName = subProject.project_name
        resp.subProjectType = subProject.subproject_type
        resp.subProjectTypeName = subProject.subproject_type_name
        
    def convert_base(self,projectSaleStrategyPo:ProjectSaleStrategyPo) -> ProjectSaleStrategyResp:
        return ProjectSaleStrategyResp(id=projectSaleStrategyPo.id,
                                       subProjectCode=projectSaleStrategyPo.sub_project_code,
                                       saleStrategyCode=projectSaleStrategyPo.sale_strategy_code,
                                       regionCode=projectSaleStrategyPo.region_code,
                                       divisionCode=projectSaleStrategyPo.division_code,
                                       dockingDate=projectSaleStrategyPo.docking_date,
                                       startDate=projectSaleStrategyPo.start_date,
                                       endDate=projectSaleStrategyPo.end_date,
                                       firstLaunchStatus=projectSaleStrategyPo.first_launch_status,
                                       firstLaunchSuccessDate=projectSaleStrategyPo.first_launch_success_date,
                                       remark=projectSaleStrategyPo.remark,
                                       createUserName=projectSaleStrategyPo.create_user_name,
                                       createTime=datetime_util.format_datetime(projectSaleStrategyPo.create_time),
                                       updateUserName=projectSaleStrategyPo.update_user_name,
                                       updateTime=datetime_util.format_datetime(projectSaleStrategyPo.update_time),
                                       )